//
//  PS_XDThreeViewController.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/11/30.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_XDThreeViewController.h"
#import "XJ_PopUpView.h"
@interface PS_XDThreeViewController ()<popViewDelegate>
{
     NSArray * cityArr;
     NSString * cId;
     NSInteger isFirst;
     NSString * stateStr;//当前房贷状态
    NSString * telephoneString;
}
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *y_ScrollView;
@property (strong, nonatomic) IBOutlet UILabel *productNameLabel;
@property (strong, nonatomic) IBOutlet UITextField *jieKuanRenTF;
@property (strong, nonatomic) IBOutlet UITextField *cardIDTF;
@property (strong, nonatomic) IBOutlet UITextField *fangChanZhengIDTF;
@property (strong, nonatomic) IBOutlet UITextField *moneyTF;
@property (strong, nonatomic) IBOutlet UITextField *phoneTF;

@property (strong, nonatomic) IBOutlet UILabel *cityLabel;
@property (strong, nonatomic) IBOutlet UIImageView *rightArrow;
@property (strong, nonatomic) IBOutlet UIButton *yiHuanQingBtn;

@property (strong, nonatomic) IBOutlet UIButton *huanKuanZhongBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *trailing_CityLabel;
@property (strong, nonatomic) IBOutlet UILabel *remarkLabel;



@property (strong, nonatomic)NSMutableArray * cityNameArray;
@property (strong, nonatomic)NSMutableArray * cityIdArray;
@property (strong, nonatomic)NSMutableArray * telephoneArray;

@end

@implementation PS_XDThreeViewController

- (NSMutableArray *)telephoneArray{
    
    if (_telephoneArray == nil) {
        
        _telephoneArray = [NSMutableArray array];
    }
    return _telephoneArray;
}

- (NSMutableArray *)cityIdArray{
    
    if (_cityIdArray == nil) {
        
        _cityIdArray = [NSMutableArray array];
    }
    
    return _cityIdArray;
}

- (NSMutableArray *)cityNameArray{
    
    if (_cityNameArray == nil) {
        
        _cityNameArray = [NSMutableArray array];
    }
    
    return _cityNameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"信贷";
    self.view.backgroundColor = RGBCOLOR(238, 238, 238);
    _y_ScrollView.constant = SafeAreaTopHeight;
    _productNameLabel.text = _proName;
    isFirst = 0;
    stateStr = @"";
    [self getSubProductData];
    
    [_cardIDTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    // Do any additional setup after loading the view from its nib.
}
- (void)getSubProductData{
    
    [Utils post:GETSUBPRODUCT parameters:@{@"sub_pid":_subId} success:^(NSInteger recode, id jsonObject) {
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];

        self->cityArr = [dataDic objectForKey:@"city2"];
        if (self->cityArr.count>0) {
            
            for (int i =0; i<self->cityArr.count; i++) {
                
                NSDictionary * dic = [self->cityArr objectAtIndex:i];
                NSString *idStr = [dic objectForKey:@"id"];
                [self.cityIdArray addObject:idStr];
                NSString * nameStr = [dic objectForKey:@"name"];
                [self.cityNameArray addObject:nameStr];
                NSString * telephone = [dic objectForKey:@"telephone"];
                [self.telephoneArray addObject:telephone];
                
            }
            
            if (self->cityArr.count == 1) {
                
                self->_rightArrow.hidden = YES;
                self->_trailing_CityLabel.constant = 20;
                self->_cityLabel.text = [self.cityNameArray firstObject];
                self->cId = [self.cityIdArray firstObject];
                self->telephoneString = [self.telephoneArray firstObject];
                
                NSString * telephoneStr = [NSString stringWithFormat:@"请添加正确的借款人信息，以免耽误审核进度，如有任何疑问请拨打:%@",self->telephoneString];
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:telephoneStr];
                NSRange range1 = [[str string] rangeOfString:@"请添加正确的借款人信息，以免耽误审核进度，如有任何疑问请拨打:"];
                [str addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(153, 153, 153) range:range1];
                NSRange range2 = [[str string] rangeOfString:self->telephoneString];
                [str addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(99, 146, 274) range:range2];
                self->_remarkLabel.attributedText =str;
            }else{
                self->_rightArrow.hidden = NO;
                self->_trailing_CityLabel.constant = 37;
            }
        }
        
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

- (IBAction)getFangDaiState:(id)sender {
    
    UIButton * button = sender;
    switch (button.tag) {
        case 1:
        {
            isFirst = 1;
            stateStr = @"还款中";
            [_huanKuanZhongBtn setImage:[UIImage imageNamed:@"tuoyuan"] forState:UIControlStateNormal];
            [_yiHuanQingBtn setImage:[UIImage imageNamed:@"tuoyuan_nor"] forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            isFirst = 2;
            stateStr = @"已还清";
            [_yiHuanQingBtn setImage:[UIImage imageNamed:@"tuoyuan"] forState:UIControlStateNormal];
            [_huanKuanZhongBtn setImage:[UIImage imageNamed:@"tuoyuan_nor"] forState:UIControlStateNormal];
        }
            break;
            
        default:
            break;
    }
    
}

- (IBAction)submitFormData:(id)sender {
    
    
    if (_cityLabel.text.length == 0) {
        
        [MBProgressHUD showError:@"请选择所在地区" toView:self.view];
        return;
    }
    
    if (_jieKuanRenTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入借款人姓名" toView:self.view];
        return;
    }
//    if (_cardIDTF.text.length == 0) {
//        
//        [MBProgressHUD showError:@"请输入借款人身份证号" toView:self.view];
//        return;
//    }
//    
//    if (_cardIDTF.text.length < 18) {
//        
//        [MBProgressHUD showError:@"请正确输入借款人身份证号" toView:self.view];
//        return;
//    }
//    
//    if (_fangChanZhengIDTF.text.length == 0) {
//        
//        [MBProgressHUD showError:@"请输入房产证号" toView:self.view];
//        return;
//    }
//    
//    if (_moneyTF.text.length == 0) {
//        
//        [MBProgressHUD showError:@"请输入月供金额" toView:self.view];
//        return;
//    }
    
    if (_phoneTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入提交人联系电话" toView:self.view];
        return;
    }
    if (_phoneTF.text.length <11) {
        
        [MBProgressHUD showError:@"请正确输入提交人联系电话" toView:self.view];
        return;
    }
    
    
    
    NSMutableDictionary * parmDic = [NSMutableDictionary dictionary];
    [parmDic setValue:@"14" forKey:@"pid"];
    [parmDic setValue:USERID forKey:@"uid"];
    [parmDic setValue:_subId forKey:@"sub_pid"];
    [parmDic setValue:cId forKey:@"city"];
    [parmDic setValue:_jieKuanRenTF.text forKey:@"name"];
    [parmDic setValue:_cardIDTF.text forKey:@"idcode"];
    [parmDic setValue:_fangChanZhengIDTF.text forKey:@"house_no"];
    [parmDic setValue:stateStr forKey:@"house_status"];
    [parmDic setValue:_moneyTF.text forKey:@"money"];
    [parmDic setValue:_phoneTF.text forKey:@"mobile"];
    
    [Utils post:APPLYSUB parameters:parmDic success:^(NSInteger recode, id jsonObject) {
        
        [MBProgressHUD showSuccess:@"申请成功" toView:self.view];
        
        int64_t delayInSeconds = 2.0; // 延迟的时间
        /*
         *  delta:
         *
         *  @parameter 1.时间参照，从此刻开始计时
         *  @parameter 2.延时多久，此处为秒级，还有纳秒等: 10ull * NSEC_PER_MSEC
         */
        
        __weak typeof(self)weakSelf = self;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
- (IBAction)getCity:(id)sender {
    
    if (self.cityNameArray.count>1) {
        
        XJ_PopUpView * popView = [[XJ_PopUpView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andTableCellWidth:230 andCellCount:self.cityNameArray.count];
        
        popView.cellArray = self.cityNameArray;
        popView.delegate = self;
        popView.type = 0;
        [self.view addSubview:popView];
    }
}
-(void)selecteCellAtIndex:(NSInteger)selectedIndex andType:(NSInteger)type{
    
    cId = [self.cityIdArray objectAtIndex:selectedIndex];
    NSString * nameStr = [self.cityNameArray objectAtIndex:selectedIndex];
    _cityLabel.text = nameStr;
    telephoneString = [self.telephoneArray objectAtIndex:selectedIndex];
    
    NSString * telephoneStr = [NSString stringWithFormat:@"请添加正确的借款人信息，以免耽误审核进度，如有任何疑问请拨打:%@",telephoneString];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:telephoneStr];
    NSRange range1 = [[str string] rangeOfString:@"请添加正确的借款人信息，以免耽误审核进度，如有任何疑问请拨打:"];
    [str addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(153, 153, 153) range:range1];
    NSRange range2 = [[str string] rangeOfString:telephoneString];
    [str addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(99, 146, 274) range:range2];
    _remarkLabel.attributedText =str;
    
}
- (void)textFieldDidChange:(UITextField *)textField

{
    
    if (textField == _phoneTF) {
        
        if (textField.text.length > 11) {
            
            textField.text = [textField.text substringToIndex:11];
            
        }
        
    }
    
    if (textField == _cardIDTF) {
        
        if (textField.text.length > 18) {
            
            textField.text = [textField.text substringToIndex:18];
            
        }
    }
    
}

- (IBAction)getPhone:(id)sender {
    
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",telephoneString];
    
    UIWebView * callWebview = [[UIWebView alloc] init];
    
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    
    [self.view addSubview:callWebview];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
